#include "Drawer.h"
#include "Sprite.h"

Drawer::Drawer()
{
	look.x = 0;
	look.y = 0;
	look.z = 0;
	at.x = 0;
	at.y = 0;
	at.z = 0;
	up.x = 0;
	up.y = 0;
	up.z = 0;
}

Drawer::~Drawer()
{
	triangles.clear();
	sprites.clear();
}

void Drawer::PushTriangle(Point p1, Point p2, Point p3)
{
	triangles.push_back(p1);
	triangles.push_back(p2);
	triangles.push_back(p3);
}

void Drawer::PushTriangle(std::vector<Point> points)
{
	for (std::vector<Point>::iterator i = points.begin(); i!=points.end(); i++)
	{
		triangles.push_back(*i);
	}
}

void Drawer::PushSprite(Sprite *sp)
{
	sprites.push_back(sp);
}

void Drawer::Draw()
{
	::glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	::glEnable(GL_DEPTH_TEST);

	GLfloat sun_light_position[] = {0.0f, -0.7f, 1.5f, 0.2f};
    GLfloat sun_light_ambient[]   = {1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat sun_light_diffuse[]   = {1.0f, 1.0f, 1.0f, 1.0f};
    GLfloat sun_light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};

    glLightfv(GL_LIGHT0, GL_POSITION, sun_light_position);
    glLightfv(GL_LIGHT0, GL_AMBIENT,   sun_light_ambient);
    glLightfv(GL_LIGHT0, GL_DIFFUSE,   sun_light_diffuse);
    glLightfv(GL_LIGHT0, GL_SPECULAR, sun_light_specular);

    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
	glEnable(GL_DEPTH_TEST);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(80,1.0f,1,3000);
	gluLookAt(100,100,100,0,0,0,0,1,0);

	/*::glBegin(GL_LINES);
	/*for(std::vector<Point>::iterator i=triangles.begin(); i!=triangles.end(); i++)
	{
		::glVertex3f(i->x, i->y, i->z);
	}
	::glColor3f(1.0f,0,0);
	::glVertex3d(0,0,0);
	::glVertex3d(100,0,0);
	::glColor3f(0,1,0);
	::glVertex3d(0,0,0);
	::glVertex3d(0,100,0);
	::glColor3f(0,0,1);
	::glVertex3d(0,0,0);
	::glVertex3d(0,0,100);
	::glEnd();*/

	for (std::vector<Sprite *>::iterator i = sprites.begin(); i!=sprites.end(); i++)
	{
		(*i)->Draw();
	}

	::glFlush();
	triangles.clear();
}

void Drawer::SetLookAt(Point _look, Point _at, Point _up)
{
	look = _look;
	at = _at;
	up = _up;
}

